package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController //组合注解：ResponseBody+Controller
@RequestMapping("/employee")
public class EmployeeController {
    //控制层依赖业务层的

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session){
        //1.获取密码，进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2. 根据username查询数据库
        //创建查询对象
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //select * from employee where username=...
        //添加查询条件
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        //通过调用mapper层的方法，获得结果
        Employee employee1 = employeeService.getOne(queryWrapper);
        //3.判断用户是否存在
        if(employee1==null){
            return R.error("用户登录失败");
        }
        //4. 用户名存在，比较密码
        if(!employee1.getPassword().equals(password)){
            return R.error("用户登录失败");
        }
        //5.查看status是否为0，如果是，返回
        if(employee1.getStatus()==0){
            return R.error("账号禁用");
        }
        //6.登录成功，将员工的id存入到session并返回登录成功结果
        session.setAttribute("employee",employee1.getId());
        return R.success(employee1);

    }

    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        //如何才是退出呢：移除session

        session.invalidate();
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpSession session){
        //将需要添加的属性值设置
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        Long uid = (Long) session.getAttribute("employee");
//        employee.setCreateUser(uid);
//        employee.setUpdateUser(uid);
        //密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employeeService.save(employee); //保存
        return R.success("添加员工成功");
    }

    //员工分页查询功能

    /**
     *
     * @param page
     * @param pageSize
     * @param name
     * @return R<Page>
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize ,String name){
        //1. 创建Page对象
        Page<Employee> employeePage = new Page<Employee>(page,pageSize);
        //2. 创建查询对象
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //2.1 添加。。。。条件
        //select * from employee where name like .... orderBy....
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getUsername,name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //3. 执行查询
        employeeService.page(employeePage,queryWrapper);//查询结果会自动封装到employeePage里面
        return R.success(employeePage);
    }
    //启用禁用功能本质上是修改
    //修改员工也是修改
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpSession session){
        log.info(employee.toString());
        //启用禁用需要设置修改人和修改时间
        Long userId = (Long) session.getAttribute("employee");//登录用户的id
        //如果是管理员账号，禁止修改
        Long id = employee.getId(); //emplyee传过来的id
        if (id==1){
            return R.error("管理员账号不能禁用");
        }
//        employee.setUpdateTime(LocalDateTime.now());//修改时间
//        employee.setUpdateUser(userId);//修改人

        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    //回显：根据要修改员工的id查询
    //{id} 是路径参数
    @GetMapping("/{id}")

    public  R<Employee> getById(@PathVariable Long id){
        log.info("根据id查询员工信息");
        Employee employee = employeeService.getById(id);
        if (employee!=null){
            return R.success(employee);
        }
        return R.error("没有查询到对应的员工信息");

    }

}
